package com.example.content.fragment.me.userproduct

import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.content.*
import com.example.content.adapter.recycle.FunAction
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.HomeItemEntity
import com.example.content.databinding.RecyclerviewLayoutBinding
import com.example.content.fragment.BaseMvvmFragment
import com.example.content.fragment.common.CommonListBindListener
import com.example.content.manager.GlobalMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.ProductInfo
import com.example.content.repository.bean.UserActionReq
import com.example.content.viewmodule.login.TokenStore
import com.example.content.viewmodule.me.MyPageCommonViewModel

/**
 * Created by zhudongao
 * Desc
 * Date 5/16/21
 */
class UserProductFragment(private val pageType: Int) :
    BaseMvvmFragment<RecyclerviewLayoutBinding, MyPageCommonViewModel>() {
    open lateinit var adapter: RecyclerAdapter<ProductInfo>
    var userId:Int = -1
    override fun initView() {
        dataBinding.refresh.setEnableLoadMore(false)
        dataBinding.refresh.setOnRefreshListener {
            queryData()
        }
        val recyclerListener = CommonListBindListener(this)
        adapter = RecyclerAdapter(recyclerListener)

        adapter.addItemViewByType(PHOTO_LIST_TYPE, R.layout.item_photo_layout)
        adapter.addItemViewByType(VIDEO_TYPE, R.layout.item_video_layout)
        dataBinding.recyclerView.adapter = adapter
        dataBinding.recyclerView.layoutManager = LinearLayoutManager(context)
        adapter.setEmptyView(R.layout.empty_pager_layout)
    }

    override fun queryData() {
        viewModel.queryUserProduct(pageType, if (userId !=  -1) userId else TokenStore.userId)
    }

    override fun initListener() {
        super.initListener()
        GlobalMgr.productDel.observe(this, {
            if (it) {
                queryData()
            }
        })
        viewModel.responseResult.observe(this, {
            dataBinding.refresh.finishRefresh()
        })
        GlobalMgr.follow.observe(this, {
            queryData()
        })
        GlobalMgr.thumbUp.observe(this, {
            if (pageType == USER_LIKE) {
                queryData()
            }
            adapter.notifyDataSetChanged()
        })
        viewModel.productResult.observe(this, {
            if (it.isEmpty()) {
                adapter.updateData(emptyList())
            } else {
                adapter.data.clear()
                for (recommend in it) {
                    val data: HomeItemEntity = when (recommend.resource_type) {
                        RESOURCE_TYPE_IMG -> {
                            HomeItemEntity(PHOTO_LIST_TYPE, recommend)
                        }
                        RESOURCE_TYPE_VIDEO -> {
                            HomeItemEntity(VIDEO_TYPE, recommend)
                        }
                        else -> {
                            HomeItemEntity(PHOTO_LIST_TYPE, recommend)
                        }
                    }
                    data.funAction = object : FunAction {
                        override fun follow(followReq: FollowReq) {
                            viewModel.follow(followReq)
                        }

                        override fun userAction(userActionReq: UserActionReq) {
                            Log.w("testidddd", "调用次数：userAction")
                            viewModel.userAction(userActionReq)
                        }
                    }
                    adapter.addData(data)
                }
                adapter.notifyDataSetChanged()
            }
        })
    }

    override fun getLayoutId() = R.layout.recyclerview_layout

    override fun getViewModuleClass(): Class<MyPageCommonViewModel> {
        return MyPageCommonViewModel::class.java
    }
}